package entity;

import game.Z5CGame;
import game.Z5CGame.Camp;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.Objects;

public class Player {

    private Session     session;
    private HttpSession httpSession;
    private Camp        camp;
    private User        registedUserInfo;
    private GameRoom    gameRoom;
    private boolean     ready;

    public Player(User userInfo) {
        this(null, null, Camp.none, userInfo);
    }

    public Player(HttpSession httpSession, Session session, Camp camp, User userInfo) {
        this.session = session;
        this.httpSession = httpSession;
        this.camp = camp;
        this.registedUserInfo = userInfo;
        this.gameRoom = null;
    }

    void leave() {
        gameRoom = null;
    }

    final boolean enterGameRoomAsChallenger(GameRoom gameRoom) {
        if(gameRoom.challengerJoin(this)) {
            this.gameRoom = gameRoom;
            return true;
        }
        return false;
    }

    final void exitGameRoomAsChallenger(GameRoom gameRoom) {
        if(gameRoom == this.gameRoom) {
            gameRoom.challengerLeave();
            setReady(false);
            leave();
        } else throw new IllegalStateException("信息不一致");
    }

    final void enterGameRoomAsHost(GameRoom gameRoom) {
        gameRoom.hostJoin(this);
        this.gameRoom = gameRoom;
    }

    final void exitGameRoomAsHost(GameRoom gameRoom) {
        if(gameRoom == this.gameRoom) {
            gameRoom.hostLeave();
            setReady(false);
            leave();
        } else throw new IllegalStateException("信息不一致");
    }

    final void enterGameRoomAsVistor(GameRoom gameRoom) {
        gameRoom.visitorJoin(this);
        this.camp = Camp.none;
        this.gameRoom = gameRoom;
    }

    final void exitGameRoomAsVistor(GameRoom gameRoom) {
        if(gameRoom == this.gameRoom) {
            gameRoom.visitorLeave(this);
            leave();
        } else throw new IllegalStateException("信息不一致");
    }

    public final GameRoom leaveRoom() {
        GameRoom room = gameRoom;
        if(gameRoom != null) {
            if(this == gameRoom.getHost()) {
                exitGameRoomAsHost(gameRoom);
                System.out.println("leavedAsHost");
            } else if(this == gameRoom.getChallenger()) {
                exitGameRoomAsChallenger(gameRoom);
                System.out.println("leavedAsChallenger");
            } else {
                exitGameRoomAsVistor(gameRoom);
                System.out.println("leavedAsVistor");
            }
        }
        return room;
    }

    public final void enterRoom(GameRoom room) {
        //如果已经加入了房间，又回到大厅点击另一个房间，则不允许直接加入（要先退出先前的房间），
        // 因而不必在服务端更新信息
        if(gameRoom != null || room == null) return;

        //不能通过这个方法直接以房主身份加入房间
        if(room.getHost() == null) return;

        if(room.getChallenger() == null) enterGameRoomAsChallenger(room);
        else enterGameRoomAsVistor(room);
    }

    public final GameRoom getGameRoom() {return gameRoom;}

    public final Session getSession() {return session;}
    public final HttpSession getHttpSession() {return httpSession;}

    public final User getUserInfo() {return registedUserInfo;}

    public final Z5CGame.Result play(String msg) {
        if(gameRoom != null) return gameRoom.play(msg, getCamp());
        return null;
    }

    @Override
    public final boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Player player = (Player) o;
        return Objects.equals(session, player.session);
    }

    @Override
    public final int hashCode() {
        return Objects.hash(session);
    }

    public final Camp getCamp() {
        return camp;
    }

    public final void setCamp(Camp camp) {
        this.camp = camp;
    }

    public final boolean isReady() { return ready; }

    public final void setReady(boolean ready) {
        if(!ready) {
            camp = Camp.none;
        }
        this.ready = ready;
    }
}
